﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes {
    import Engine.Transactions.*;
    import flash.display.*;
    import Engine.Helpers.*;
    import Mechanics.*;
    import org.aswing.*;
    import Classes.util.*;
    import Classes.sim.*;
    import __AS3__.vec.*;
    import Classes.LogicComponents.*;
    import Modules.bandits.*;
    import GameMode.*;
    import Classes.actions.*;
    import Mechanics.GameEventMechanics.*;
    import Modules.stats.types.*;
    import Modules.zoo.*;
    import Modules.peddler.peddlerMarket.*;

    public class Municipal extends SimpleHarvestableResource implements IPopulationStateObserver, IPlayerStateObserver {

        public static const COMPONENTS:Object = {
            police:MunicipalComponentPolice,
            upgradable:MunicipalComponentUpgradable,
            peddlercenter:MunicipalComponentPeddlerCenter,
            zoo:MunicipalComponentZoo
        };
        public static const DEFAULT_MUNICIPAL_IMAGE:String = "static";

        private const MUNICIPAL:String = "municipal";

        protected var m_logic:MunicipalComponentBase = null;

        public function Municipal(_arg1:String){
            super(_arg1);
            if (this.m_logic == null){
                this.m_logic = this.getLogicComponent();
            };
            m_objectType = WorldObjectTypes.MUNICIPAL;
            m_typeName = this.MUNICIPAL;
        }
        override public function loadObject(_arg1:Object):void{
            super.loadObject(_arg1);
            this.m_logic.onLoadObject(_arg1);
        }
        override public function setId(_arg1:Number):void{
            super.setId(_arg1);
            this.m_logic.onSetId(_arg1);
        }
        override public function setItem(_arg1:Item):void{
            super.setItem(_arg1);
            if (this.m_logic == null){
                this.m_logic = this.getLogicComponent();
            };
        }
        override public function attach():void{
            super.attach();
            Global.world.citySim.addObserver(this);
            Global.player.addObserver(this);
        }
        override public function detach():void{
            super.detach();
            Global.world.citySim.removeObserver(this);
            Global.player.removeObserver(this);
        }
        public function getLogicComponent():MunicipalComponentBase{
            var _local1:Class;
            var _local2:MunicipalComponentBase;
            if (COMPONENTS.hasOwnProperty(m_item.behavior)){
                _local1 = COMPONENTS[m_item.behavior];
                _local2 = (new _local1(this) as MunicipalComponentBase);
                return (_local2);
            };
            return (new MunicipalComponentBase(this));
        }
        public function levelChanged(_arg1:int):void{
            this.setState(getState());
            this.m_logic.onLevelChanged(_arg1);
        }
        public function xpChanged(_arg1:int):void{
        }
        public function commodityChanged(_arg1:int, _arg2:String):void{
        }
        public function energyChanged(_arg1:Number):void{
        }
        public function goldChanged(_arg1:int):void{
        }
        public function cashChanged(_arg1:int):void{
        }
        public function onPopulationInit(_arg1:int, _arg2:int, _arg3:int):void{
            this.m_logic.onPopulationInit(_arg1, _arg2, _arg3);
        }
        public function onPopulationChanged(_arg1:int, _arg2:int, _arg3:int, _arg4:int):void{
            this.setState(getState());
            this.m_logic.onPopulationChanged(_arg1, _arg2, _arg3, _arg4);
        }
        public function onPotentialPopulationChanged(_arg1:int, _arg2:int):void{
        }
        public function onPopulationCapChanged(_arg1:int):void{
        }
        override protected function statsInit():void{
            super.statsInit();
            registerStatsActionName(TrackedActionType.FIRST_PLANT, "grandopening");
            registerStatsActionName(TrackedActionType.HARVEST, "collect");
            registerStatsActionName(TrackedActionType.PLANT, "open");
        }
        override public function onBuildingConstructionCompleted_PreServerUpdate():void{
            Global.world.citySim.recomputePopulationCap(Global.world);
            var _local1:Number = getGrowTimeDelta();
            this.m_plantTime = (GlobalEngine.getTimer() - _local1);
            this.setState(STATE_PLANTED);
            plantTime = GlobalEngine.getTimer();
        }
        override public function onBuildingConstructionCompleted_PostServerUpdate():void{
            this.m_logic.onBuildingConstructionCompleted();
        }
        override public function onSell():void{
            Global.world.citySim.recomputePopulationCap(Global.world);
        }
        override protected function getCurrentImage():ItemImageInstance{
            var _local1:ItemImageInstance = m_item.getCachedImage(DEFAULT_MUNICIPAL_IMAGE, this, m_direction, m_statePhase);
            return (_local1);
        }
        override public function isCurrentImageLoading():Boolean{
            if (m_item == null){
                return (false);
            };
            return (m_item.isCachedImageLoading(DEFAULT_MUNICIPAL_IMAGE, m_direction, m_statePhase));
        }
        override public function isDrawImageLoading():Boolean{
            if (m_item == null){
                return (false);
            };
            return (m_item.isCachedImageLoading(DEFAULT_MUNICIPAL_IMAGE, m_direction, m_statePhase));
        }
        override public function getToolTipStatus():String{
            return (this.m_logic.getToolTipStatus());
        }
        override public function getToolTipAction():String{
            return (this.m_logic.getToolTipAction());
        }
        override public function getCustomToolTipStatus():Component{
            var _local3:Number;
            var _local1:Component = super.getCustomToolTipStatus();
            var _local2:Boolean;
            if (_local1){
                if (((((!(Global.isVisiting())) && ((getState() == HarvestableResource.STATE_PLANTED)))) && (!(isUpgradePossible())))){
                    _local2 = true;
                };
                if (_local2){
                    _local3 = (getToolTipTextWidth(ZLoc.t("Main", "BareResidence", {time:"00:00:00"})) + 5);
                    if (_local3 > _local1.getPreferredWidth()){
                        _local1.setPreferredWidth(_local3);
                        _local1.setMaximumWidth(_local3);
                    };
                } else {
                    _local1.setPreferredSize(null);
                };
            };
            return (_local1);
        }
        override public function getCustomToolTipLevelBar():Component{
            return (null);
        }
        override public function getHighlightColor():uint{
            var _local1:Boolean = ((this.isHarvestable()) && ((Global.world.getTopGameMode() is GMPlay)));
            var _local2:Boolean = ((this.isUpgradePossible()) && ((Global.world.getTopGameMode() is GMPlay)));
            return ((_local1) ? EmbeddedArt.READY_HIGHLIGHT_COLOR : (_local2) ? EmbeddedArt.UPGRADE_HIGHLIGHT_COLOR : super.getHighlightColor());
        }
        override public function onPlayAction():void{
            this.m_logic.handlePlayAction();
        }
        override public function harvest():Boolean{
            Global.player.heldEnergy = (Global.player.heldEnergy - m_heldEnergy);
            return (super.harvest());
        }
        override public function doHarvestDropOff(_arg1:Boolean=true):Object{
            var _local2:Object = super.doHarvestDropOff(_arg1);
            this.m_logic.doHarvestDropOff(_arg1);
            return (_local2);
        }
        override public function getProgressBarCancelFunction():Function{
            return (function ():void{
                Global.player.heldEnergy = (Global.player.heldEnergy - m_heldEnergy);
            });
        }
        override public function getProgressBarEndFunction():Function{
            var _local1:Function = this.m_logic.getProgressBarEndFunction();
            return (((_local1)!=null) ? _local1 : super.getProgressBarEndFunction());
        }
        override protected function makeDoobers(_arg1:Number=1):Array{
            return (Global.player.processRandomModifiers(harvestingDefinition, this, true, m_secureRands));
        }
        override protected function createStagePickEffect():void{
            this.m_logic.createStagePickEffect();
        }
        override protected function updateArrow():void{
            if (this.m_logic.enableUpdateArrow()){
                this.createStagePickEffect();
            } else {
                removeStagePickEffect();
            };
        }
        override public function lock():void{
            super.lock();
            this.updateArrow();
        }
        override public function unlock():void{
            super.unlock();
            this.updateArrow();
        }
        override public function upgradeBuildingIfPossible(_arg1:Boolean=true, _arg2:Transaction=null, _arg3:Boolean=true):void{
            this.m_logic.upgradeBuildingIfPossible();
        }
        override public function onObjectDrop(_arg1:Vector3):void{
            super.onObjectDrop(_arg1);
            this.updateArrow();
        }
        override public function rotate():void{
            super.rotate();
            this.updateArrow();
        }
        override public function replaceContent(_arg1:DisplayObject):void{
            super.replaceContent(_arg1);
            this.updateArrow();
        }
        override public function setState(_arg1:String):void{
            super.setState(_arg1);
            this.updateArrow();
        }
        override protected function getItemImageHotspots(_arg1:Item, _arg2:String, _arg3:int, _arg4:String=""):Vector.<Vector3>{
            return (_arg1.getCachedImageHotspots(DEFAULT_MUNICIPAL_IMAGE, m_direction, _arg4));
        }
        override public function getItemImageDimensions():Vector3{
            if (m_item){
                return (m_item.getCachedImageDimensions(DEFAULT_MUNICIPAL_IMAGE, m_direction));
            };
            return (null);
        }
        override public function prepareForStorage():void{
            super.prepareForStorage();
            Global.world.citySim.recomputePopulationCap(Global.world);
        }
        override public function restoreFromStorage():void{
            super.restoreFromStorage();
            Global.world.citySim.recomputePopulationCap(Global.world);
        }
        override public function get unlimitedVisits():Boolean{
            return (true);
        }
        public function get rollCallTieredDooberValue():int{
            var _local4:Object;
            var _local1:RollCallDataMechanic = (MechanicManager.getInstance().getMechanicInstance(this, "rollCall", MechanicManager.ALL) as RollCallDataMechanic);
            var _local2:Array = _local1.getCrewState();
            var _local3:int;
            for each (_local4 in _local2) {
                if (_local4.checkedIn){
                    _local3++;
                };
            };
            return (_local3);
        }
        public function set rollCallTieredDooberValue(_arg1:int):void{
        }
        override public function doUpgradeAnimations(_arg1:Function=null, _arg2:Boolean=true):void{
            var _local3:String = ZLoc.t("Main", "Upgrading");
            this.doProgressBar(_local3, _arg1);
        }
        public function doProgressBar(_arg1:String, _arg2:Function=null):void{
            this.actionQueue.removeAllStates();
            this.actionQueue.addActions(new ActionProgressBar(null, this, _arg1), new ActionFn(this, this.updateArrow));
            if (_arg2 != null){
                this.actionQueue.addActions(new ActionFn(this, _arg2));
            };
            Sounds.playFromSet(Sounds.SET_BUILDING_CONSTRUCTION);
        }

    }
}//package Classes 
